package com.github.io;

import java.io.*;


/**
 * @author wangyadong
 * @date 2021/7/30 16:20
 */
public class OutPutFileDataTradition implements OutputFileData {


    public OutPutFileDataTradition() {

    }

    @Override
    public String[] list(String path) {
        File file = new File(path);
        return file.list();
    }


    @Override
    public void outPutFile(byte[] bytes, File file) {
        if(!file.exists()){
            try {
                file.mkdirs();
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (OutputStream outputStream = new FileOutputStream(file)) {
            outputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void outPutFile(byte[] bytes, String path, String name) {
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            String filePath = path + name;
            file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }

            OutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String path) {
        File file = new File(path);
        file.delete();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        OutPutFileDataTradition outPutFileDataTradition = new OutPutFileDataTradition();
        String value = "  {\"dataSource\": \"icome\",\"dataTable\": \"hr_valuecreation_individual_board_h\",\"filter\": {\"fields\": [{ \"field\": \"year\", \"type\": \"selector\", \"value\": \"2021\"},{  \"field\": \"pernr\", \"type\": \"selector\",\"value\": \"10106573\" },{ \"field\": \"quarter\", \"type\": \"in\", \"values\": [ \"Q3\" ] }, { \"field\": \"month\", \"type\": \"in\", \"values\": [\"07\", \"08\", \"09\" ]}, { \"field\": \"kpi_id\", \"type\": \"in\", \"values\": [\"kpi_019\", \"kpi_031\" ] } ], \"type\": \"and\" }, \"limitLower\": 0,  \"limitUpper\": 100, \"queryType\": \"query\", \"returnFields\": [ \"pernr\",  \"month\",   \"quarter\",    \"year\",   \"kpi_id\",   \"kpi_note\",   \"value\" ], \"userKey\": \"16e74400-8359-4d13-ace7-b9d98fb2a687\"}]";
        outPutFileDataTradition.outPutFile(value.getBytes(), "D://", "value-test-qutoe.txt");
    }


}
